Notes on the semantics of programming languages

The semantics (the meaning) of a programming language (or better, the meaning of the programs written in that programming language) is defined by the semantics of the syntactic constructs, the semantics associated with the non-terminal symbols and the production rules. For example, the meaning of a constant definition (in a program) is the value of the constant; the meaning of the definition of a data type is normally the set of values that belong to the type; the meaning of an expression is the value of that expression; and the meaning of a statement (possibly composed statement) is the effect that its execution has on the values stored in the program variables in memory.

Certain semantic properties can be evaluated during the compilation phase; they are called static semantics. For instance, in a language with static typing, the type of the value of an expression can be evaluated by the compiler, but the value of an expression depends in general on the values of variables which will only be known during the execution phase.

The static semantics is often represented by semantic attributes associated with the non-terminals. These attributes will be evaluated by the compiler and their values will be associated with the non-terminal nodes within the syntax tree. For example, each node <expression> in the syntax tree my have an attribute, called "type", which contains the value of the type of the expression. For more details, see the book by Sebesta, Section 3.5.

Different methods can be used to define the dynamic semantics of a programming language, that is, those aspects of "meaning" that is defined in reference to the execution phase of the program. These methods can be classified into the following categories:

An example: The definition of the semantics of regular expressions